package lab5;

import java.awt.*;
import javax.swing.*;
import java.util.*;

/**
 * A panel containing two bouncing balls.  This panel may be placed in a JFrame.
 * @author ahanes
 */
public class BallPanel extends JPanel {

    // Ball 1
    int b1xCoord;
    int b1yCoord;
    int b1Height;
    int b1Width;
    int b1Rise;
    int b1Run;
    Color b1Color;
    
    // Ball 2
    int b2xCoord;
    int b2yCoord;
    int b2Height;
    int b2Width;
    int b2Rise;
    int b2Run;
    Color b2Color;
    
    /** Creates a new instance of BallPanel */
    public BallPanel() {
        super();
        
        // Initialize Ball 1
        b1xCoord = 0;
        b1yCoord = 0;
        b1Height = 15;
        b1Width = 15;
        b1Rise = 3;
        b1Run = 6;
        b1Color = Color.BLUE;
        
        // Initialize Ball 2
        b2xCoord = 10;
        b2yCoord = 20;
        b2Height = 10;
        b2Width = 10;
        b2Rise = 5;
        b2Run = 4;
        b2Color = Color.GREEN;        
        
    }
    
    /**
        Paints the balls at their current positions within the panel.
     */
    public void paintComponent (Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.black);                         // set color black
        g.fillRect(0,0,this.getWidth(),this.getHeight()); // paint background

        // Paint Ball 1
        g.setColor(b1Color);
        g.fillOval(b1xCoord, b1yCoord, b1Width, b1Height);

        // Paint Ball 2
        g.setColor(b2Color);
        g.fillOval(b2xCoord, b2yCoord, b2Width, b2Height);
    } // end method paintComponent   

    /**
       Computes the next position for the balls and updates their positions.
     */
    public void move () {
        // Move Ball 1
        // If ball is approaching a wall, reverse direction
        if (b1xCoord < (0 - b1Run) || b1xCoord > (getWidth() - b1Width)) { 
            b1Run = -b1Run; 
        }
        
	if (b1yCoord < (0 - b1Rise) || b1yCoord > (getHeight() - b1Height)) { 
            b1Rise = -b1Rise; 
        }
        
        // "Move" ball according to values in rise and run
        b1xCoord += b1Run;
        b1yCoord += b1Rise;
        
        
        // Move Ball 2
        // If ball is approaching a wall, reverse direction
        if (b2xCoord < (0 - b2Run) || b2xCoord > (getWidth() - b2Width)) { 
            b2Run = -b2Run; 
        }
        
	if (b2yCoord < (0 - b2Rise) || b2yCoord > (getHeight() - b2Height)) { 
            b2Rise = -b2Rise; 
        }
        
        // "Move" ball according to values in rise and run
        b2xCoord += b2Run;
        b2yCoord += b2Rise;
        
    } // end method move
    
}
